package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.SoulQuestionOption;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.SoulQuestionOptionMapper;
import com.itheima.service.db.SoulQuestionOptionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
public class SoulQuestionOptionServiceImpl implements SoulQuestionOptionService {

    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;

    //根据问题的id查找选项
    @Override
    public List<SoulQuestionOption> findByQuestionId(Long id) {
        QueryWrapper<SoulQuestionOption> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", id);
        List<SoulQuestionOption> optionList = soulQuestionOptionMapper.selectList(wrapper);
        return optionList;
    }

    //查分数
    @Override
    public SoulQuestionOption findScore(Long questionId, Long optionId) {
        QueryWrapper<SoulQuestionOption> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", questionId);
        wrapper.eq("id", optionId);
        SoulQuestionOption soulQuestionOption = soulQuestionOptionMapper.selectOne(wrapper);
        return soulQuestionOption;
    }

    //查找选项列表
    @Override
    public PageBeanVo findOptionList(Integer page, Integer pagesize) {
        IPage page1 = new Page(page, pagesize);

        QueryWrapper<SoulQuestionOption> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("question_id");

        page1 = soulQuestionOptionMapper.selectPage(page1, wrapper);

        return new PageBeanVo(page, pagesize, page1.getTotal(), page1.getRecords());
    }


    //保存选项
    @Override
    public String save(SoulQuestionOption soulQuestionOption) {
        soulQuestionOptionMapper.insert(soulQuestionOption);
        return "ok";
    }

    //删除选项
    @Override
    public void deleteOptions(Long id) {
        soulQuestionOptionMapper.deleteById(id);
    }

    //回显
    @Override
    public SoulQuestionOption findById(Long id) {
        SoulQuestionOption soulQuestionOption = soulQuestionOptionMapper.selectById(id);
        return soulQuestionOption;
    }

    //修改
    @Override
    public void updateOptions(SoulQuestionOption soulQuestionOption) {
        soulQuestionOptionMapper.updateById(soulQuestionOption);
    }
}
